package com.xd.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.xd.service.DzStudentService;
import com.xd.util.AjaxResult;
import com.xd.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created with IDEA
 *
 * @author xp
 * @date 2019/2/26 14:05
 */
@Api(tags = "学生端登录")
@Controller
@RequestMapping("/")
public class DzStudentController {
    @Autowired
    private DzStudentService dzStudentService;
    @Autowired
    private Producer producer;

     @ApiOperation(value = "生成验证码")
     @RequestMapping(value = "/captcha.jpg", method = GET)
    public void captcha(HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    /**
     *  用户登录
     * @param account  账号名称
     * @param password  密码
     * @param captcha  验证码
     * @return
     */
    @ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
    @ApiImplicitParam(name = "id", value = "学生ID", paramType = "path", required = true, dataType = "Integer")
    @RequestMapping(value = "/login", method =POST, produces = "application/json")
    @ResponseBody
    public AjaxResult login(String account, String password, String captcha){
        String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if(!captcha.equalsIgnoreCase(kaptcha)){
            return AjaxResult.fail("验证码不正确");
        }
        try{
            //  获取Subject
            Subject subject=ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(account, password);
            subject.login(token);
        }catch (UnknownAccountException e) {
            return AjaxResult.fail(e.getMessage());
        }catch (IncorrectCredentialsException e) {
            return  AjaxResult.fail("账号或密码不正确");
        }catch (LockedAccountException e) {
            return  AjaxResult.fail("账号已被锁定,请联系管理员");
        }catch (AuthenticationException e) {
            return  AjaxResult.fail("账户验证失败");
        }
        return  AjaxResult.success("用户登录成功");
    }




}
